﻿
#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;

#endregion

namespace FasterWPF
{
    public class ToolBarTrayArgs
    {
        public int Band { get; set; }
        public int BandIndex { get; set; }
        public ToolBarTrayArgs(int band, int indexWithinABand)
        {
            Band = band;
            BandIndex = indexWithinABand;
        }
    }

    public class ToolBarTrayParent : FasterWPF.IParent
    {         
        //this is ANA?
        public object Generate<T>(object chain, T args, Action<ToolBarTray> addUIElementsAction)
        {
            return chain;
        }

        //this is BIND
        public object Add<T>(object chain, string key, T args, Border itemToAdd)
        {            
            ToolBar tbar = ((ToolBar)(((StackPanel)(((ContentControl)itemToAdd.Child).Content))).Children[0]);
            ContentControl cc =  new ContentControl();
            cc.Tag = tbar;

            (((StackPanel)((ContentControl)itemToAdd.Child).Content)).Children.Remove(tbar);
            (((StackPanel)((ContentControl)itemToAdd.Child).Content)).Children.Add(cc);

            if (args is ToolBarTrayArgs)
            {
                if ((args as ToolBarTrayArgs).Band != null)
                {
                    tbar.Band = (args as ToolBarTrayArgs).Band;
                    tbar.BandIndex = (args as ToolBarTrayArgs).BandIndex;
                }
            }

            ((ToolBarTray)chain).ToolBars.Add(tbar);
            return chain;
        }

        public void Remove(object chain, string key, Border itemToRemove)
        {
            ContentControl cc = ((ContentControl)(((StackPanel)(((ContentControl)itemToRemove.Child).Content))).Children[0]);
            
            ((ToolBarTray)chain).ToolBars.Remove(cc.Tag as ToolBar); //remove from ToolBarTray

            (((StackPanel)((ContentControl)itemToRemove.Child).Content)).Children.Clear();
        }

        public Dictionary<string, object> RetrieveDictionary(object chain)
        {
            return ((ToolBarTray)chain).GetCompositeLookupDictionary<ToolBarTray>();
        }

        //this is CATA?
        public List<Border> RetrieveCollection(object chain)
        {
            List<Border> lb = new List<Border>();
            foreach (var kvp in ((ToolBarTray)chain).GetCompositeLookupDictionary<ToolBarTray>())
            {
                lb.Add((Border)kvp.Value);
            }

            return lb;
        }

        public void ClearCollection(object chain)
        {
        }

        public void BeforeSortCollection<T>(object chain, object args, int row, int column, FasterWPF.CommonExt.SortOrder sortOrder)
                  where T : FrameworkElement
        { }

        public void BeforeSortCollection1Then2<T>(object chain, object args, int row, int column, int row2, int column2, FasterWPF.CommonExt.SortOrder sortOrder, FasterWPF.CommonExt.SortOrder sortOrder2)
  where T : FrameworkElement
        { }
        
        public void AfterSortCollection<T>(object chain)
             where T : FrameworkElement
        { }

        public void InnerSortCollection(object chain, int row, int column, string key, ref int counter, Border itemToReAdd)
        {
            throw new NotImplementedException();
        }
        
        public void SetSelected(object chain, Border itemToSetAsSelected)
        {
        }

       
    }
}
